package com.ajie.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONUtil;
import com.ajie.config.annotation.RestControllerMapping;
import com.ajie.entity.BaseEntity;
import com.ajie.entity.dto.Option;
import com.ajie.model.PageParams;
import com.ajie.model.Result;
import com.ajie.entity.FoodType;
import com.ajie.service.FoodTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * 食物分类;(food_type)表控制层
 * @author: ajie
 * @date : 2024-7-27
 */
@Tag(name = "食物分类")
@RestControllerMapping("/foodType")
@ApiSupport(author = "ajie:20999@163.com", order = 9)
@SaCheckRole(value = { "ADMIN", "FOOD_ADMIN" }, mode = SaMode.OR)
public class FoodTypeController {

    @Resource
    private FoodTypeService foodTypeService;

    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<FoodType>> pageQuery(@RequestBody PageParams<FoodType> pageParams) {
        // 1.查询参数
        FoodType params = pageParams.getParams();
        LambdaQueryWrapper<FoodType> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.eq(params.getTitle() != null, FoodType::getTitle, params.getTitle());
        // 3.分页查询
        return Result.success(foodTypeService.page(pageParams.getPage(), lqw));
    }

    @GetMapping("options")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "列表")
    public Result<List<Option>> options() {
        // 1.查询参数
        QueryWrapper<FoodType> lqw = new QueryWrapper<>();
        lqw.select("title as label", "id as value");
        // 3.分页查询
        List<Map<String, Object>> maps = foodTypeService.listMaps(lqw);
        List<Option> options = JSONUtil.parseArray(maps).toList(Option.class);
        return Result.success(options);
    }

    @PostMapping("save")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission("food:type:save")
    @Operation(summary = "保存数据", description = "角色：ADMIN, FOOD_ADMIN,拥有其中一个角色<br>权限：food:type:save")
    public Result<Boolean> insert(@RequestBody FoodType foodType) {
        if (foodTypeService.saveOrUpdate(foodType)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission("food:type:delete")
    @Operation(summary = "通过主键删除数据", description = "角色：ADMIN, FOOD_ADMIN,拥有其中一个角色<br>权限：food:type:delete", parameters = {
            @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        if (foodTypeService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }
}